//
//  AppDelegate.m
//  HomeIn
//
//  Created by mac on 15/11/4.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "AppDelegate.h"
#import "LaunchViewController.h"
#import "FirstPageViewController.h"
#import "ActivityViewController.h"
#import "AudioVisualViewController.h"
#import "UserViewController.h"
#import "AudioVisualDetailViewController.h"
@interface AppDelegate ()<UITabBarControllerDelegate>



@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //  判断是否第一次进入这个APP,如果是,在沙盒里面存放一个文件,再次打开这个APP时如果有次文件,则不用加载引导页
    NSArray *fileArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [fileArray lastObject];
    NSString *archivePath = [documentPath stringByAppendingPathComponent:@"yindaoye.txt"];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL isHasFile = [manager fileExistsAtPath:archivePath];
    
    if (isHasFile) {
        //  为真表示已有文件 曾经进入过主页
        [self goMain];
    } else {
        //  为假表示没有文件，没有进入过主页
        LaunchViewController *launchVC = [[LaunchViewController alloc] init];
        self.window.rootViewController = launchVC;
        
        isHasFile = YES;
    }
 
    return YES;
}

- (void)goMain
{
    
    FirstPageViewController *firstPageVC = [[FirstPageViewController alloc]init];
    UINavigationController *firstPageNaVC = [[UINavigationController alloc]initWithRootViewController:firstPageVC];
    firstPageNaVC.navigationBar.translucent = NO;
    firstPageNaVC.navigationBar.barTintColor = RGBA(251, 106, 10, 1);
    firstPageNaVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"1"] tag:10000];
    
    
    ActivityViewController *activityVC = [[ActivityViewController alloc]init];
    UINavigationController *activityNaVC = [[UINavigationController alloc]initWithRootViewController:activityVC];
    activityNaVC.navigationBar.translucent = NO;
    activityNaVC.navigationBar.barTintColor = RGBA(251, 106, 10, 1);
    activityNaVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"分类" image:[UIImage imageNamed:@"2"] tag:10002];
    
    
    
    AudioVisualViewController *audioVisualVC = [[AudioVisualViewController alloc]init];
    UINavigationController *audioVisualNaVC = [[UINavigationController alloc]initWithRootViewController:audioVisualVC];
    audioVisualNaVC.navigationBar.translucent = NO;
    audioVisualNaVC.navigationBar.barTintColor = RGBA(251, 106, 10, 1);
    audioVisualNaVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"电台" image:[UIImage imageNamed:@"5"] tag:10003];

    UserViewController *userVC = [[UserViewController alloc]init];
    UINavigationController *userNaVC = [[UINavigationController alloc]initWithRootViewController:userVC];
    userNaVC.navigationBar.translucent = NO;
    userNaVC.navigationBar.barTintColor = RGBA(251, 106, 10, 1);
    userNaVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"3"] tag:10004];
    
    
    UITabBarController *tabBarC = [[UITabBarController alloc]init];
    tabBarC.viewControllers = [NSArray arrayWithObjects:firstPageNaVC,activityNaVC,audioVisualNaVC,userNaVC, nil];
    tabBarC.tabBar.barTintColor = [UIColor wheatColor];
    tabBarC.tabBar.tintColor = RGBA(251, 106, 10, 1);//设置选中颜色
    tabBarC.tabBar.translucent = NO;//是否取消半透明效果，no是没有半透明效果
    //协议代理
    tabBarC.delegate = self;
    self.window.rootViewController = tabBarC;

    
    [[DataHandle shareDataBaseHandle]openDB];
    [[DataHandle shareDataBaseHandle]createAllTable];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
//    [[AudioVisualDetailViewController shareAudioVisualDetailViewComtroller] playButtonClick];
//    if ([AudioVisualDetailViewController shareAudioVisualDetailViewComtroller].player.state == STKAudioPlayerStatePlaying) {
//        [[AudioVisualDetailViewController shareAudioVisualDetailViewComtroller].player pause];
//    }
    
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    if (event.type == UIEventTypeRemoteControl) {
        if (event.subtype == UIEventSubtypeRemoteControlPlay) {
            [[AudioVisualDetailViewController shareAudioVisualDetailViewComtroller] playButtonClick];
        }
        if (event.subtype == UIEventSubtypeRemoteControlPause) {
            [[AudioVisualDetailViewController shareAudioVisualDetailViewComtroller] playButtonClick];
        }
        if (event.subtype == UIEventSubtypeRemoteControlStop) {
            [[AudioVisualDetailViewController shareAudioVisualDetailViewComtroller] stopButtonClick];
        }
        [[AudioVisualDetailViewController shareAudioVisualDetailViewComtroller] newMusicInfo];
        
    }
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    UIBackgroundTaskIdentifier identifer = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:identifer];
        
    }];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
//    NSLog(@"2");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//    [[AudioVisualDetailViewController shareAudioVisualDetailViewComtroller] playButtonClick];
    if ([AudioVisualDetailViewController shareAudioVisualDetailViewComtroller].player.state == STKAudioPlayerStatePaused) {
        [[AudioVisualDetailViewController shareAudioVisualDetailViewComtroller].player resume];
        [AudioVisualDetailViewController shareAudioVisualDetailViewComtroller].playButton.selected = YES;
    }
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
